#ifndef DATABASETHREAD_H__
#define DATABASETHREAD_H__

#include <queue>
#include "thread.h"

namespace engine
{
	class SqlOperation;
	class IQueryCallback;
	class QueryResult;
	class QueryResultSet;

	//////////////////////////////////////////////////////////////////////////
	class DatabaseThread : protected Thread
	{
	public:
		virtual void* entry();

	public:
		DatabaseThread();
		virtual ~DatabaseThread();

		void start();
		void stop();

		uint32 getQueueSize();

		bool push(SqlOperation* op);

		void handleResults();

		void pushResult(IQueryCallback* callback);

	public:
		virtual QueryResultSet* query(const String& sql) = 0;
		virtual bool execute(const String& sql) = 0;

	protected:
		typedef std::queue<SqlOperation*> SqlQueue;
		SqlQueue* mPushSqlQueue;
		SqlQueue* mPopSqlQueue;
		CriticalSection mSqlCS;
		Condition mSqlCond;
		Mutex mSqlMutex;

		typedef std::queue<IQueryCallback*> ResultQueue;
		ResultQueue* mPushResultQueue;
		ResultQueue* mPopResultQueue;
		CriticalSection mResultCS;

		volatile uint32 mStop;
		uint32 mPopQueueSize;
	};

}

#endif
